package ans.p66;

import java.util.Arrays;

/**
 * #      66
 * 名称   加一
 * 链接   https://leetcode-cn.com/problems/plus-one
 * 题解   [2401](https://leetcode-cn.com/problems/plus-one/solution)
 * 通过率 45.7%
 * 难道   简单
 */
class Solution {
    public int[] plusOne(int[] digits) {
        boolean addOne = true;
        for (int i = digits.length - 1; i >= 0; i--) {
            if (!addOne) {
                break;
            }
            if (digits[i] + 1 == 10) {
                digits[i] = 0;
                addOne = true;
            } else {
                digits[i] = digits[i] + 1;
                addOne = false;
            }
        }
        if (addOne) {
            int[] ints = new int[digits.length + 1];
            ints[0] = 1;
            for (int i = 1; i < ints.length; i++) {
                ints[i] = digits[i - 1];
            }
            return ints;
        } else {
            return digits;
        }
    }
}

